﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KC.DataAccess.MV;

namespace KC.Presentation.Mv8CMS
{
	public partial class SessionManager : Form
	{
		public SessionManager()
		{
			InitializeComponent();
		}

		private void SessionManager_Load(object sender, EventArgs e)
		{
			DataTable dt = KC.DataAccess.MV.Utils.GetMonthlySessions();
			foreach (DataRow dr in dt.Rows) {
				DateTime
					startDay = DateTime.Parse(dr[0].ToString().Split('-')[1] + "/01/" + dr[0].ToString().Split('-')[0]),
					endDay = startDay.AddMonths(1).AddSeconds(-1);
				int Hits = int.Parse(dr[1].ToString());
				dynamic obj = new {
					startday = startDay,
					stopday = endDay,
					Sessions = Hits,
					Text = startDay.ToString("MMMM yyyy") + "  (" + Hits.ToString("###,###,###") + ")" };
				lbMonths.Items.Add(obj);
			}
		}

		private void lbMonths_SelectedIndexChanged(object sender, EventArgs e)
		{
			dynamic obj = (dynamic)lbMonths.SelectedItem;
			IQueryable<Session> result = KC.DataAccess.MV.Utils.GetSessionsByTimes(obj.startday, obj.stopday);

			List<dynamic> result2 = new List<dynamic>();
			foreach (Session session in result.AsEnumerable<Session>())
			{
				dynamic obj2 = new {
					ID = session.ID,
					dStarted = session.dStarted,
					dEnded = session.dEnded,
					IP = session.IP.Address
				};
				result2.Add(obj2);
			}


			dgSessions.DataSource = result2;
		}

		private void dgSessions_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			new ActivityPeeker((int)(dgSessions.Rows[e.RowIndex].Cells[0].Value)).ShowDialog();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}
	}
}
